<?php

namespace app\api\model;

use app\common\enums\StatusEnum;

class OauthRecite extends \app\common\model\OauthRecite
{
    /**
     * 收藏本1 错题本2 对应的单词或者句子 单词或句子id => 记录id
     * @param $oauthId
     * @param $type
     * @param $reciteIds
     * @return array
     */
    public static function getListByTypeColumn($oauthId, $type, $reciteIds = [])
    {
        $sql = static::where([
            'oauth_id' => $oauthId,
            'type' => $type
        ]);
        if ($reciteIds) {
            $sql->where('recite_id', 'in', $reciteIds);
        }
        return $sql->column('id', 'recite_id');
    }

    /**
     * 收藏本 或者 错题本列表
     * @param int $oauthId
     * @param int $type 收藏本1 错题本2
     * @param int $reciteType 背单词1 背句子2
     * @param $classId
     * @param $courseType
     * @param int $page 第几页
     * @param int $size 每页显示条数
     * @return array
     * @throws \think\Exception\DbException
     */
    public static function getListByType($oauthId, $type, $reciteType, $classId, $courseType, $page, $size)
    {
        $offset = ($page - 1) * $size;
        $where = [
            'a.oauth_id' => $oauthId,
            'lr.status' => StatusEnum::ENABLED,
            'lr.type' => $reciteType,
            'a.type' => $type,
            'lr.class_id' => $classId,
            'lr.course_type' => $courseType
        ];

        $sql = static::field('a.id AS id, lr.id AS reciteId, lr.word')
            ->alias('a')
            ->join('lang_class_recite lr', 'lr.id = a.recite_id')
            ->where($where);

        $model = $sql->order(['a.create_time' => 'desc'])
            ->limit($offset, $size)
            ->select()
            ->toArray();

        return [
            'list' => $model,
            'count' => $sql->count()
        ];
    }

    /**
     * 获取 背单词 或者 背句子详情
     * @param $id
     * @param $oauthId
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($id, $oauthId = 0)
    {
        $where = [
            'lr.status' => StatusEnum::ENABLED,
            'a.id' => $id
        ];
        $model = static::field('a.id, lr.file, a.recite_id AS reciteId, lr.word')
            ->alias('a')
            ->join('lang_class_recite lr', 'lr.id = a.recite_id')
            ->where($where)
            ->find();
        if ($model) {
            // 判断是否已经收藏
            $collect = OauthRecite::getListByTypeColumn($oauthId, OauthRecite::TYPE_COLLECT, [$model['reciteId']]);
            $model['isCollect'] = $collect ? 1 : 0;
            $model['file'] = config('file_prefix') . $model['file'];
            $answers = LangClassReciteAnswer::getListByRecite([$model['reciteId']]);
            $model['answers'] = isset($answers[$model['reciteId']]) ? $answers[$model['reciteId']] : [];
        }
        return $model ? $model : [];
    }

    /**
     * 通过单词或者句子ID获取一条记录
     * @param $type
     * @param $oauthId
     * @param $reciteId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOneBy($type, $oauthId, $reciteId)
    {
        $where = [
            'lr.status' => StatusEnum::ENABLED,
            'a.type' => $type,
            'a.oauth_id' => $oauthId,
            'a.recite_id' => $reciteId
        ];
        $model = static::field('a.id')
            ->alias('a')
            ->join('lang_class_recite lr', 'lr.id = a.recite_id')
            ->where($where)
            ->find();
        return $model ? $model->toArray() : [];
    }

    /**
     * 批量插入错题集记录 或者 收藏记录
     * @param $oauthId
     * @param $type
     * @param $ids
     * @throws \Exception
     */
    public static function addBatch($oauthId, $type, $ids)
    {
        // 获取已经存在的数据
        $column = self::getListByTypeColumn($oauthId, $type);

        $insert = [];
        foreach ($ids as $reciteId) {
            if (!isset($column[$reciteId])) {
                $insert[$reciteId] = [
                    'oauth_id' => $oauthId,
                    'type' => $type,
                    'recite_id' => $reciteId
                ];
            }
        }

        // 批量插入
        if ($insert) {
            $recite = new OauthRecite();
            $recite->saveAll($insert);
        }
    }

    /**
     * 移除错题本 或者 收藏本 指定记录
     * @param $id
     */
    public static function deleteOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->delete();
        }
    }
}